package com.sdhs.projectschedule.controller;

import com.sdhs.common.annotation.Log;
import com.sdhs.common.core.controller.BaseController;
import com.sdhs.common.core.domain.AjaxResult;
import com.sdhs.common.core.page.TableDataInfo;
import com.sdhs.common.enums.BusinessType;
import com.sdhs.projectschedule.domain.GcProgressSupply;
import com.sdhs.projectschedule.service.IGcProgressSupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目进度控制--供货填报-主Controller
 *
 * @author ww
 */
@RestController
@RequestMapping("/progress/supply")
public class GcProgressSupplyController extends BaseController {
    @Autowired
    private IGcProgressSupplyService gcProgressSupplyService;

    /**
     * 查询项目进度控制--供货填报-主列表
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:list')")
    @GetMapping("/list")
    public TableDataInfo list(GcProgressSupply gcProgressSupply) {
        List<GcProgressSupply> list = gcProgressSupplyService.selectGcProgressSupplyList(gcProgressSupply);
        return getDataTable(list);
    }
    /**
     * 查询项目进度控制-供货填报-主列表
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:totalList')")
    @GetMapping("/totalList")
    public Map<String, Double> totalList(GcProgressSupply gcProgressSupply) {
        List<GcProgressSupply> list = gcProgressSupplyService.selectGcProgressSupplyList(gcProgressSupply);
        // 获取总数
        double totalProgress = 0;
        for (GcProgressSupply item : list) {
            try {
                if (item.getSupplySchedule() != null) {
                    totalProgress += Double.parseDouble(item.getSupplySchedule());
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }

        // 如果总值大于100强制设置为100
        totalProgress = Math.min(totalProgress, 100);
        // 使用100减去总项目进度
        double remainingProgress = 100 - totalProgress;
        //保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        totalProgress = Double.parseDouble(df.format(totalProgress));
        remainingProgress = Double.parseDouble(df.format(remainingProgress));

        Map<String, Double> result = new HashMap<>();
        result.put("totalProgress", totalProgress);
        result.put("remainingProgress", remainingProgress);
        return result;
    }
    /**
     * 获取项目进度控制--供货填报-主详细信息
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(gcProgressSupplyService.selectGcProgressSupplyById(id));
    }

    /**
     * 新增项目进度控制--供货填报-主
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:add')")
    @Log(title = "项目进度控制--供货填报-主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GcProgressSupply gcProgressSupply) {
        return toAjax(gcProgressSupplyService.insertGcProgressSupply(gcProgressSupply));
    }

    /**
     * 修改项目进度控制--供货填报-主
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:edit')")
    @Log(title = "项目进度控制--供货填报-主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GcProgressSupply gcProgressSupply) {
        return toAjax(gcProgressSupplyService.updateGcProgressSupply(gcProgressSupply));
    }

    /**
     * 删除项目进度控制--供货填报-主
     */
    @PreAuthorize("@ss.hasPermi('progress:supply:remove')")
    @Log(title = "项目进度控制--供货填报-主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(gcProgressSupplyService.deleteGcProgressSupplyByIds(ids));
    }
}
